﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall.Input;
using TopDownShooter.Screens;
using FlatRedBall;

namespace TopDownShooter
{
    class Weapon
    {
        protected Unit m_Unit;
        public Weapon(Unit owner)
        {
            m_Unit = owner;
            bulletsCurrentlyInClip = clipSize;
        }
        internal int clipSize = 10;
        internal int clipCount = 10;
        internal int bulletsCurrentlyInClip = 0;
        internal int startingClipCount = 10;
        internal float accuracy = 1.5f;
        internal float secondsBetweenBullets = 0.17f;
        internal float secondsSinceLastBullet = 1000;
        internal int damage = 1;

        protected String weaponType = "";

        internal float secondsToReload = 1;
        internal float secondsSinceReloadStart = 0;
        internal bool reloading = false;
        public virtual void Activity()
        {
            if (reloading && clipCount > 0)
            {
                if(typeof(Player) == m_Unit.GetType())
                    m_Unit.Screen.Hud.SetReloadingProgress(secondsSinceReloadStart, secondsToReload);
                if (secondsSinceReloadStart >= secondsToReload)
                {
                    bulletsCurrentlyInClip = clipSize;
                    clipCount--;
                    reloading = false;
                    secondsSinceReloadStart = 0;
                    UpdateAmmo();
                }
                secondsSinceReloadStart += TimeManager.SecondDifference;
            }
            secondsSinceLastBullet += TimeManager.SecondDifference;
        }
        public virtual void Selected()
        {
            UpdateAmmo();
        }
        public virtual void EmitBullet()
        {
            Bullet newBullet = new Bullet();
            newBullet.Fire(m_Unit.RotationZ + (float)((GameScreen.Random.NextDouble() * accuracy) / 10) - (accuracy / 17), m_Unit.Position);
            m_Unit.bulletsFired.Add(newBullet);
            m_Unit.Screen.allBullets.Add(newBullet);
            newBullet.Source = m_Unit;
            newBullet.Damage = damage;
        }
        public virtual void Fire()
        {
            if (reloading || bulletsCurrentlyInClip <= 0 || secondsSinceLastBullet < secondsBetweenBullets)
            {
                return;
            }
            bulletsCurrentlyInClip--;
            if (bulletsCurrentlyInClip <= 0)
                // Reload!
                reloading = true;
            EmitBullet();
            UpdateAmmo();
            secondsSinceLastBullet = 0;
        }
        public virtual void DoMouseInput()
        {
            if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                Fire();
        }

        protected void UpdateAmmo()
        {
            if(typeof(Player) == m_Unit.GetType())
                m_Unit.Screen.Hud.SetAmmo(weaponType, bulletsCurrentlyInClip, clipCount);
        }
    }

    class Pistol : Weapon
    {
        public Pistol(Unit owner)
            : base(owner)
        {
            clipSize = 15;
            clipCount = startingClipCount = 10;
            accuracy = .3f;
            damage = 30;
            secondsBetweenBullets = 0.1f;
            bulletsCurrentlyInClip = clipSize;
            weaponType = "PISTOL";
            UpdateAmmo();
        }
    }

    class SMG : Weapon
    {
        public SMG(Unit owner)
            : base(owner)
        {
            clipSize = 60;
            clipCount = startingClipCount = 6;
            accuracy = 2.3f;
            secondsBetweenBullets = 0.1f;
            damage = 10;
            bulletsCurrentlyInClip = clipSize;
            weaponType = "SMG";
            UpdateAmmo();

        }
        public override void DoMouseInput()
        {
            if (InputManager.Mouse.ButtonDown(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                Fire();
            base.Activity();
        }

    }

    class Shotgun : Weapon
    {
        public Shotgun(Unit owner)
            : base(owner)
        {
            clipSize = 1;
            clipCount = startingClipCount = 15;
            accuracy = 4f;
            secondsBetweenBullets = .2f;
            bulletsCurrentlyInClip = clipSize;
            secondsToReload = 2;
            weaponType = "SHOTGUN";
            damage = 20;
            UpdateAmmo();
        }

        public override void EmitBullet()
        {
            for(int i = 0; i < 9; i++)
                base.EmitBullet();
        }
        public override void DoMouseInput()
        {
            if (InputManager.Mouse.ButtonDown(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                Fire();
            base.Activity();
        }
    }
}
